#!/bin/bash

# ===================================================================
# Mac版本的Docker项目文件拷贝工具
# 复制项目的文件到对应docker路径，便于一键生成镜像
# ===================================================================

echo "===================================================="
echo "           开始拷贝项目文件到Docker目录..."
echo "           (自动清理旧文件，确保使用最新版本)"
echo "===================================================="
echo

# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd "$SCRIPT_DIR"

echo "当前目录: $(pwd)"
echo

# 检查是否存在必要的源目录
if [ ! -d "../hello-ui/dist" ]; then
    echo "[WARNING] 未找到 ../hello-ui/dist 目录，请先构建前端项目"
fi

if [ ! -d "../hello-gateway/target" ]; then
    echo "[WARNING] 未找到 ../hello-gateway/target 目录，请先编译后端项目"
fi

if [ ! -d "../hello-auth/target" ]; then
    echo "[WARNING] 未找到 ../hello-auth/target 目录，请先编译后端项目"
fi

if [ ! -d "../hello-modules/hello-system/target" ]; then
    echo "[WARNING] 未找到 ../hello-modules/hello-system/target 目录，请先编译后端项目"
fi

echo

# copy html
echo "[INFO] 开始拷贝前端文件..."
if [ -d "../hello-ui/dist" ]; then
    # 清理目标目录
    if [ -d "./nginx/dist" ]; then
        echo "[INFO] 清理旧的前端文件..."
        rm -rf ./nginx/dist/*
    fi
    mkdir -p ./nginx/dist
    cp -r ../hello-ui/dist/* ./nginx/dist/
    if [ $? -eq 0 ]; then
        echo "[SUCCESS] 前端文件拷贝完成"
    else
        echo "[ERROR] 前端文件拷贝失败"
    fi
else
    echo "[SKIP] 跳过前端文件拷贝（源目录不存在）"
fi
echo

# copy jar files
echo "[INFO] 开始拷贝hello-gateway..."
if [ -f "../hello-gateway/target/hello-gateway.jar" ]; then
    # 清理旧的JAR文件
    if [ -f "./hello/gateway/jar/hello-gateway.jar" ]; then
        echo "[INFO] 清理旧的hello-gateway.jar..."
        rm -f ./hello/gateway/jar/hello-gateway.jar
    fi
    mkdir -p ./hello/gateway/jar
    cp ../hello-gateway/target/hello-gateway.jar ./hello/gateway/jar/
    if [ $? -eq 0 ]; then
        echo "[SUCCESS] hello-gateway.jar 拷贝完成"
    else
        echo "[ERROR] hello-gateway.jar 拷贝失败"
    fi
else
    echo "[SKIP] 跳过hello-gateway.jar拷贝（文件不存在）"
fi
echo

echo "[INFO] 开始拷贝hello-auth..."
if [ -f "../hello-auth/target/hello-auth.jar" ]; then
    # 清理旧的JAR文件
    if [ -f "./hello/auth/jar/hello-auth.jar" ]; then
        echo "[INFO] 清理旧的hello-auth.jar..."
        rm -f ./hello/auth/jar/hello-auth.jar
    fi
    mkdir -p ./hello/auth/jar
    cp ../hello-auth/target/hello-auth.jar ./hello/auth/jar/
    if [ $? -eq 0 ]; then
        echo "[SUCCESS] hello-auth.jar 拷贝完成"
    else
        echo "[ERROR] hello-auth.jar 拷贝失败"
    fi
else
    echo "[SKIP] 跳过hello-auth.jar拷贝（文件不存在）"
fi
echo

echo "[INFO] 开始拷贝hello-modules-system..."
if [ -f "../hello-modules/hello-system/target/hello-modules-system.jar" ]; then
    # 清理旧的JAR文件
    if [ -f "./hello/modules/system/jar/hello-modules-system.jar" ]; then
        echo "[INFO] 清理旧的hello-modules-system.jar..."
        rm -f ./hello/modules/system/jar/hello-modules-system.jar
    fi
    mkdir -p ./hello/modules/system/jar
    cp ../hello-modules/hello-system/target/hello-modules-system.jar ./hello/modules/system/jar/
    if [ $? -eq 0 ]; then
        echo "[SUCCESS] hello-modules-system.jar 拷贝完成"
    else
        echo "[ERROR] hello-modules-system.jar 拷贝失败"
    fi
else
    echo "[SKIP] 跳过hello-modules-system.jar拷贝（文件不存在）"
fi
echo

# 显示拷贝结果总结
echo "===================================================="
echo "           文件拷贝操作完成！"
echo "===================================================="
echo

# 检查拷贝结果
echo "[INFO] 检查拷贝结果："
if [ -d "./nginx/dist" ] && [ "$(ls -A ./nginx/dist)" ]; then
    echo "✓ 前端文件已拷贝到: ./nginx/dist"
else
    echo "✗ 前端文件拷贝失败或目录为空"
fi

if [ -f "./hello/gateway/jar/hello-gateway.jar" ]; then
    echo "✓ Gateway JAR已拷贝到: ./hello/gateway/jar/"
else
    echo "✗ Gateway JAR拷贝失败"
fi

if [ -f "./hello/auth/jar/hello-auth.jar" ]; then
    echo "✓ Auth JAR已拷贝到: ./hello/auth/jar/"
else
    echo "✗ Auth JAR拷贝失败"
fi

if [ -f "./hello/modules/system/jar/hello-modules-system.jar" ]; then
    echo "✓ System JAR已拷贝到: ./hello/modules/system/jar/"
else
    echo "✗ System JAR拷贝失败"
fi

echo
echo "现在可以运行 docker-compose build 来构建镜像了"
echo
echo "按任意键退出..."
read -n 1 -s